{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "06609140-8145-4795-b093-85ac20ccbd48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "a    0\n",
      "c    2\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "d = {'b':1,'a':0,'c':2}\n",
    "s = pd.Series(d)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "edb16d77-9b70-4b89-8fe6-dda24b31c331",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Series([], dtype: object)\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "s = pd.Series()\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ef4098a-a14c-49ec-b2ba-3855965a1aa3",
   "metadata": {},
   "source": [
    "ndarry是Numpy中的数据类型，当data是ndarry时，传递的索引必须具有与数组相同的长度，假设没有给index参数传参，在默认情况，索引值将使用range（n）生成，其中n代表数组长度,默认索引从零开始，其索引范围从零到len（data）-1，这种设置方式被称为隐式索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9fc379d3-98b8-43bc-955e-8f79e787fdb0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    a\n",
      "1    b\n",
      "2    c\n",
      "3    d\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "data = np.array(['a','b','c','d'])\n",
    "s = pd.Series(data)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "37383e8c-a5b0-424a-9f0e-2d8672cd16a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    a\n",
      "2    b\n",
      "3    c\n",
      "4    d\n",
      "dtype: object\n",
      "101    a\n",
      "102    b\n",
      "103    c\n",
      "104    d\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "data = np.array(['a','b','c','d'])\n",
    "# 自定义显式索引\n",
    "s = pd.Series(data,index=[1,2,3,4])\n",
    "print(s)\n",
    "s = pd.Series(data,index=[101,102,103,104])\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0c1ec393-9469-48c7-810d-6a8c60367623",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1.0\n",
      "c    2.0\n",
      "d    NaN\n",
      "a    0.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "data = {'a':0,'b':1,'c':2}\n",
    "s = pd.Series(data,index = ['b','c','d','a'])\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0eb8528c-d21d-4edb-9f7b-9bb6a0cac875",
   "metadata": {},
   "source": [
    "当传递的索引值无法找到与其对应的值时，使用NAN（非数字）填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2321d8b6-3ffb-451a-9723-9829f74c1f73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    5\n",
      "1    5\n",
      "2    5\n",
      "3    5\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 使用标量创建series对象\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "s = pd.Series(5,index=[0,1,2,3])\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b5b1aeb-c49d-476d-833d-44aee6d7579a",
   "metadata": {},
   "source": [
    "访问series数据\n",
    "分为两种方式:一是位置索引访问，二是索引标签访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e7a7d376-b993-4bf6-9052-548e6aea3f90",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\abc18\\AppData\\Local\\Temp\\ipykernel_11244\\1451338080.py:4: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  print(s[0])\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "s = pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])\n",
    "print(s[0])  # 位置下标，访问的是1\n",
    "print(s['a'])  # 标签下标，访问的是1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d200d525-a220-4b9b-a9de-f5e3ec3b2306",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "dtype: int64\n",
      "c    3\n",
      "d    4\n",
      "e    5\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 通过切片的方法访问series序列的数据\n",
    "import pandas as pd\n",
    "s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])\n",
    "print(s[:3])  # 取前三个\n",
    "print(s[-3:])  # 取后三个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c2efdd6b-702a-4560-8224-6d696eb3e54a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "# 索引标签访问\n",
    "import pandas as pd\n",
    "s = pd.Series([6,7,8,9,10],index = ['a','b','c','d','e'])\n",
    "print(s['a'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "81fe9eb3-f8b1-4a2c-80a3-0ee708030df3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    6\n",
      "b    7\n",
      "c    8\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "s = pd.Series([6,7,8,9,10],index = ['a','b','c','d','e'])\n",
    "print(s[['a','b','c']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b330aee9-314c-4e86-a42d-768b52671d9f",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'f'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "File \u001b[1;32mD:\\anaconda3\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:3805\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   3804\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 3805\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_engine\u001b[38;5;241m.\u001b[39mget_loc(casted_key)\n\u001b[0;32m   3806\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n",
      "File \u001b[1;32mindex.pyx:167\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32mindex.pyx:196\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32mpandas\\\\_libs\\\\hashtable_class_helper.pxi:7081\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32mpandas\\\\_libs\\\\hashtable_class_helper.pxi:7089\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'f'",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 4\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpandas\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mpd\u001b[39;00m\n\u001b[0;32m      3\u001b[0m s \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mSeries([\u001b[38;5;241m6\u001b[39m,\u001b[38;5;241m7\u001b[39m,\u001b[38;5;241m8\u001b[39m,\u001b[38;5;241m9\u001b[39m,\u001b[38;5;241m10\u001b[39m],index \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mc\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124md\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124me\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[1;32m----> 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(s[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m])\n",
      "File \u001b[1;32mD:\\anaconda3\\Lib\\site-packages\\pandas\\core\\series.py:1121\u001b[0m, in \u001b[0;36mSeries.__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   1118\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_values[key]\n\u001b[0;32m   1120\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m key_is_scalar:\n\u001b[1;32m-> 1121\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_value(key)\n\u001b[0;32m   1123\u001b[0m \u001b[38;5;66;03m# Convert generator to list before going through hashable part\u001b[39;00m\n\u001b[0;32m   1124\u001b[0m \u001b[38;5;66;03m# (We will iterate through the generator there to check for slices)\u001b[39;00m\n\u001b[0;32m   1125\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_iterator(key):\n",
      "File \u001b[1;32mD:\\anaconda3\\Lib\\site-packages\\pandas\\core\\series.py:1237\u001b[0m, in \u001b[0;36mSeries._get_value\u001b[1;34m(self, label, takeable)\u001b[0m\n\u001b[0;32m   1234\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_values[label]\n\u001b[0;32m   1236\u001b[0m \u001b[38;5;66;03m# Similar to Index.get_value, but we do not fall back to positional\u001b[39;00m\n\u001b[1;32m-> 1237\u001b[0m loc \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mindex\u001b[38;5;241m.\u001b[39mget_loc(label)\n\u001b[0;32m   1239\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_integer(loc):\n\u001b[0;32m   1240\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_values[loc]\n",
      "File \u001b[1;32mD:\\anaconda3\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:3812\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   3807\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(casted_key, \u001b[38;5;28mslice\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m (\n\u001b[0;32m   3808\u001b[0m         \u001b[38;5;28misinstance\u001b[39m(casted_key, abc\u001b[38;5;241m.\u001b[39mIterable)\n\u001b[0;32m   3809\u001b[0m         \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28many\u001b[39m(\u001b[38;5;28misinstance\u001b[39m(x, \u001b[38;5;28mslice\u001b[39m) \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m casted_key)\n\u001b[0;32m   3810\u001b[0m     ):\n\u001b[0;32m   3811\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m InvalidIndexError(key)\n\u001b[1;32m-> 3812\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[0;32m   3813\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[0;32m   3814\u001b[0m     \u001b[38;5;66;03m# If we have a listlike key, _check_indexing_error will raise\u001b[39;00m\n\u001b[0;32m   3815\u001b[0m     \u001b[38;5;66;03m#  InvalidIndexError. Otherwise we fall through and re-raise\u001b[39;00m\n\u001b[0;32m   3816\u001b[0m     \u001b[38;5;66;03m#  the TypeError.\u001b[39;00m\n\u001b[0;32m   3817\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_indexing_error(key)\n",
      "\u001b[1;31mKeyError\u001b[0m: 'f'"
     ]
    }
   ],
   "source": [
    "# 如果使用了index中不包含的标签，则会触发异常\n",
    "import pandas as pd\n",
    "s = pd.Series([6,7,8,9,10],index = ['a','b','c','d','e'])\n",
    "print(s['f'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d1e6f6b-ab01-40bf-86b3-11d36061a298",
   "metadata": {},
   "source": [
    "series常用属性\n",
    "axes 以列表的形式返回所有行索引标签\n",
    "dtype 返回对象的数据类型\n",
    "empty 返回一个空的series对象\n",
    "nidm 返回输入数据的维度\n",
    "size 返回输入数据的元素数量\n",
    "values 以ndarry的形式返回series对象\n",
    "index返回一个rangeeindex对象，用来描述索引的取值范围"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "09dcd1ae-958e-4c1f-baad-32365ca150ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.548063\n",
      "1    0.661725\n",
      "2    0.734847\n",
      "3   -0.681473\n",
      "4   -1.613884\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "s = pd.Series(np.random.randn(5))\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b8dc13dd-583d-4c61-b9b6-d8b7cbd44df2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[RangeIndex(start=0, stop=5, step=1)]\n",
      "float64\n",
      "0   -0.484247\n",
      "1    0.371449\n",
      "2   -1.718808\n",
      "3   -0.754297\n",
      "4   -1.516651\n",
      "dtype: float64\n",
      "是否为空对象\n",
      "False\n",
      "输出series中的数据\n",
      "[-0.48424698  0.37144921 -1.7188077  -0.7542971  -1.5166507 ]\n"
     ]
    }
   ],
   "source": [
    "# 以列表的形式返回所有的行索引标签\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "s = pd.Series(np.random.randn(5))\n",
    "print(s.axes)\n",
    "print(s.dtype)  # 返回对象的数据类型\n",
    "# 判断数据对象是否为空，返回的是布尔值\n",
    "print(s)\n",
    "print(\"是否为空对象\")\n",
    "print(s.empty)\n",
    "# print(s.nidm) \n",
    "# 以数组的方式返回series对象中的数据\n",
    "print('输出series中的数据')\n",
    "print(s.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "606869f0-c012-46ec-987f-96a3b55aab32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['a', 'b', 'c', 'd'], dtype='object')\n",
      "RangeIndex(start=0, stop=4, step=1)\n"
     ]
    }
   ],
   "source": [
    "# 查看series当中属性的取值范围\n",
    "# 显示索引\n",
    "import pandas as pd\n",
    "s = pd.Series([1,2,5,8],index = ['a','b','c','d'])\n",
    "print(s.index)\n",
    "# 隐式索引\n",
    "s1 = pd.Series([1,2,5,8])\n",
    "print(s1.index)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9f7bebc-a71f-4094-ab79-e6b6c11f5f1f",
   "metadata": {},
   "source": [
    "series常用方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "70dca4dd-c9f6-4500-8b70-d6f91c0818fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -2.668498\n",
      "1   -0.696819\n",
      "2    0.984773\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# head(),tail() 查看数据,可以使用head（）或者tail（）方法，其中head（）返回前n行的数据，默认显示前5行\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "s = pd.Series(np.random.randn(999))\n",
    "print(s.head(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cb885847-88a8-4f73-8cbf-4e7a83970abf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.120381\n",
      "1   -1.764576\n",
      "2    1.138193\n",
      "3   -2.244233\n",
      "dtype: float64\n",
      "............\n",
      "2    1.138193\n",
      "3   -2.244233\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# tail（）返回的是后n行数据，默认是后5行\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "s = pd.Series(np.random.randn(4))\n",
    "print(s)\n",
    "print('............')\n",
    "print(s.tail(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b1e11e2f-5ea7-4723-b50a-c97f06facb57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3     True\n",
      "dtype: bool\n",
      "0     True\n",
      "1     True\n",
      "2     True\n",
      "3    False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "source": [
    "# isnull（），notnull（）检测缺失值（值不存在，丢失，缺少）\n",
    "# isnull如果值不存在或者缺失，则返回true。notnull返回false\n",
    "import pandas as pd\n",
    "# None代表缺失数据\n",
    "s = pd.Series([1,2,3,None])\n",
    "print(pd.isnull(s))  # true\n",
    "print(pd.notnull(s))  # false"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
